package com.woniu.cq.mallrocketmq.controller;

import com.woniu.cq.mallrocketmq.dto.MessageDto;
import com.woniu.cq.mallrocketmq.dto.Product;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author Daniel
 * @version 1.0.0
 * @ClassName ProducerController
 * @Description TODO
 * @createTime 2020年09月23日 17:49:00
 */
@RestController
public class ProducerController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    /**
     * @description: 同步消息
     * @param: message
     * @param: tag
     * @return: java.lang.String
     */
    @GetMapping("/sendMessage/{message}/{tag}")
    public String sendMessage(@PathVariable("message") String message, @PathVariable("tag") String tag) {

        /*Message<String> msg = MessageBuilder.withPayload(message)
                .setHeader("KEYS", UUID.randomUUID().toString())
                .build();
        for (int i = 0; i < 10; i++) {
            rocketMQTemplate.syncSend("topic:" + tag, msg);
        }*/
        MessageDto messageDto = new MessageDto();
        messageDto.setBody(message);
        Message<MessageDto> msg = MessageBuilder.withPayload(messageDto)
                .setHeader("KEYS", UUID.randomUUID().toString())
                .build();
        rocketMQTemplate.syncSend("topic:" + tag, msg);
        return "起飞";
    }

    /**
     * @description: 异步消息
     * @param: message
     * @param: tag
     * @return: java.lang.String
     */
    @GetMapping("/sendMessage1/{message}/{tag}")
    public String sendMessage1(@PathVariable("message") String message, @PathVariable("tag") String tag) {
        Product product = new Product();
        product.setInfo(message);
        product.setName("大罗卜");
        product.setPrice(200.0);
        MessageDto messageDto = new MessageDto();
        messageDto.setBody(product);
        Message<MessageDto> msg = MessageBuilder.withPayload(messageDto)
                .setHeader("KEYS", UUID.randomUUID().toString())
                .build();
        rocketMQTemplate.asyncSend("topic:" + tag, msg, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println(sendResult);
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println(throwable.getMessage());
            }
        });
        return "起飞";
    }
    /** 
     * @description:  顺序消息
     * @param: message
     * @param: tag 
     * @return: java.lang.String 
     */
    @GetMapping("/sendMessage2/{message}/{tag}")
    public String sendMessage2(@PathVariable("message") String message, @PathVariable("tag") String tag) {
        Product product = new Product();
        product.setInfo(System.currentTimeMillis()+"");
        product.setName(message);
        product.setPrice(200.0);
        MessageDto messageDto = new MessageDto();
        messageDto.setBody(product);

        Message<MessageDto> msg = MessageBuilder.withPayload(messageDto)
                .setHeader("KEYS", UUID.randomUUID().toString())
                .build();
        for (int i = 0; i < 100; i++) {

            rocketMQTemplate.asyncSendOrderly("topic:"+tag,msg,"hashKey", new SendCallback() {
                @Override
                public void onSuccess(SendResult sendResult) {
                    System.out.println(sendResult);
                }

                @Override
                public void onException(Throwable throwable) {
                    System.out.println(throwable.getMessage());
                }
            });
        }
        return "起飞";
    }

}
